package com.github_Dididipapa.shortlink.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.github_Dididipapa.shortlink.admin.common.result.R;
import com.github_Dididipapa.shortlink.admin.common.result.Rs;
import com.github_Dididipapa.shortlink.admin.dao.entity.User;
import com.github_Dididipapa.shortlink.admin.dto.req.UserLoginReqDTO;
import com.github_Dididipapa.shortlink.admin.dto.resp.UserActualRespDTO;
import com.github_Dididipapa.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @description: To be defined
 * @author: dididi_papa
 * @email: mitis1624087624@gmail.com
 * @date: 2025/09/05  11:19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/short-link/admin/v1")
public class UserController {
    private final UserService userService;

    /**
     * @description: 根据用户名查找用户信息
     * @author: dididi_papa
     * @date: 2025/9/9 下午2:30
     * @param: [username]
     * @return: com.github_Dididipapa.shortlink.admin.dao.entity.User
     **/
    @GetMapping("/user/{username}")
    public R getUserByUsername(@PathVariable("username") String username) {
        return userService.getUserByUsername(username);
    }

    /**
     * @description: 查找用户名无脱敏信息
     * @author: dididi_papa
     * @date: 2025/9/9 下午2:34
     * @param: [username]
     * @return: com.github_Dididipapa.shortlink.admin.dao.entity.User
     **/
    @GetMapping("/actual/user/{username}")
    public R<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Rs.success(BeanUtil.toBean(userService.getUserByUsername(username).getData(), UserActualRespDTO.class));
    }

    /**
     * @description: 查询用户名是否可用
     * @author: dididi_papa
     * @date: 2025/9/9 下午2:34
     * @param:
     * @return:
     **/
    @GetMapping("/user/has-username")
    public R hasUsername(@RequestParam("username") String username) {
        System.out.println(username);
        return userService.existUsername(username);
    }

    /**
     * @description: 注册用户
     * @author: dididi_papa
     * @date: 2025/9/9 下午2:40
     * @param:
     * @return:
     **/
    @PostMapping("/user")
    public R registerUser(@RequestBody User user) throws IOException {
        return userService.registerUser(user);
    }

    /**
     * @description: 修改用户信息
     * @author: dididi_papa
     * @date: 2025/9/9 下午3:15
     * @param: [user]
     * @return: com.github_Dididipapa.shortlink.admin.common.result.R
     **/
    @PutMapping("/user")
    public R updateUser(@RequestBody User user) {
        return userService.updateById(user) ? Rs.success() : Rs.failure();
    }

    /**
     * @description: 用户登录
     * @author: dididi_papa
     * @date: 2025/9/9 下午3:15
     * @param: [user]
     * @return: com.github_Dididipapa.shortlink.admin.common.result.R
     **/
    @PostMapping("/user/login")
    public R<String> login(@RequestBody UserLoginReqDTO user) {
        return Rs.success(userService.login(user).getToken());
    }

    /**
     * @description: 检查用户是否登录
     * @author: dididi_papa
     * @date: 2025/9/9 下午4:54
     * @param: [token, usrername]
     * @return: com.github_Dididipapa.shortlink.admin.common.result.R<java.lang.String>
     **/
    @GetMapping("/user/check-login")
    public R<Boolean> checkLogin(@RequestParam("token") String token,
                                 @RequestParam("username") String usrername) {
        return Rs.success(userService.checkLogin(token, usrername));
    }


    @DeleteMapping("/user/logout")
    public R<Boolean> logOut(@RequestParam("token") String token,
                             @RequestParam("username") String usrername) {
        return Rs.success(userService.logOut(token, usrername));
    }
}
